package com.clp.config.execptionHandler;

import com.clp.utils.Result;
import com.clp.utils.log.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Create by chenlipeng on 2022/01/26 10:13
 */

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    //指定出现什么异常执行这个方法
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        return Result.error().message("执行了全局异常处理...");
    }
    
    //特定异常处理
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error(ArithmeticException e){
        e.printStackTrace();
        return Result.error().message("执行了ArithmeticException异常处理...");
    }


    //自定义异常处理
    @ExceptionHandler(GuLiException.class)
    @ResponseBody
    public Result error(GuLiException e){
        log.error(ExceptionUtil.getMessage(e));
        
        e.printStackTrace();
        return Result.error().code(e.getCode()).message(e.getMsg());
    }
}
